<?php
class Controller {

    private static $_instance;
    protected $_view;
    protected $_model;
    protected $_library;
    protected $_cookie;
    protected $_session;
    protected $_post;
    protected $_get;
    protected $_token;

    public function __construct() {
        //Load the default library.
        self::$_instance = &$this;
        $this->_view = array();
        $this->_model = array();
        $this->_library = array();
        $this->startSession();
        $this->_cookie = $_COOKIE;
        $this->_post = $_POST;
        $this->_get = $_GET;
        if(empty($this->_token)){
            $this->_token = $_SESSION['token'] = md5('sywo2013');
        }
        $this->_session = $_SESSION;
    }

    public function __destruct() {
        ob_end_flush();
    }

    public static function &getInstace() {
        return self::$_instance;
    }

    public function template($name, $return = FALSE, $params = null) {
        if (trim($name) == '') {
            echo "Name of template is empty.";
            return false;
        }
        if (!$return) {
            include 'views/' . str_replace('.php', '', $name) . '.php';
            //echo file_get_contents('views/' . str_replace('.php', '', $name) . '.php');
        } else {
            return file_get_contents('views/' . str_replace('.php', '', $name) . '.php');
        }

        $this->_view[$name] = $name;
    }

    public function library($name = '', $nameObject = '', array $params = null) {
        if ((bool) trim($name) == '') {
            echo 'Name of library is empty.';
            return false;
        }

        if (!(bool) file_exists('libraries/' . str_replace('.php', '', $name) . '.php')) {
            echo 'library is not existed.';
            return false;
        }

        require_once 'libraries/' . str_replace('.php', '', $name) . '.php';

        if ((bool) empty($nameObject)) {
            $nameObject = $name;
        }

        $name = ucfirst($name);
        if ((bool) is_null($params)) {
            $this->$nameObject = new $name();
        } else {
            $this->$nameObject = new $name($params);
        }

        if ((bool) is_null($this->$nameObject)) {
            echo 'Can not initialize object of this library.';
            return false;
        }

        $this->_library[$name] = $name;
    }

    public function model($name = '', $nameObject = '', array $params = null) {
        if ((bool) trim($name) == '') {
            echo 'Name of model is empty.';
            return false;
        }

        if (!(bool) file_exists('models/' . str_replace('.php', '', $name) . '.php')) {
            echo 'Model is not existed.';
            return false;
        }

        require_once 'models/' . str_replace('.php', '', $name) . '.php';

        if ((bool) empty($nameObject)) {
            $nameObject = $name;
        }

        $name = ucfirst($name);
        if ((bool) is_null($params)) {
            $this->$nameObject = new $name();
        } else {
            $this->$nameObject = new $name($params);
        }

        if ((bool) is_null($this->$nameObject)) {
            echo 'Can not initialize object of this model.';
            return false;
        }

        $this->_model[$name] = $name;
    }

    public function startSession() {
        ob_start();
        session_start();
    }
}

?>
